package edu.westga.movierec.controllers;
import edu.westga.movierec.model.*;
/**
 * Controller class that allows administrators to
 * fundamentally affect the state of the movie database
 * 
 * @author jmcook
 *
 */
public class AdminController {
	private XamppDBConnector theDatabase;
	/**
	 * Creates a new instance of the recommender interface 
	 * that will gets its data from a Xampp database
	 */
	public AdminController(){
		theDatabase = new XamppDBConnector();
	}
	//***********Movie Window Methods*******************
	/**
	 * Deletes all references to a movie from the database
	 */
	public void deleteMovieFromDatabase(String movieTitle){
		if(!isInDatabase(movieTitle)){
			return;
		}
		theDatabase.deleteMovieFromDatabase(movieTitle);
	}
	/**
	 * Adds all needed references to a new movie so database
	 * can properly function
	 */
	public void addMovieToDatabase(String movieTitle){
		if(isInDatabase(movieTitle)){
			return;
		}
		theDatabase.addMovieToDatabase(movieTitle);
	}
	/**
	 * Changes the rating for two movies to a user specs
	 */
	public void changeMovieRating(String input, String output, int rating){
		if(!isInDatabase(input) || !isInDatabase(output)){
			return;
		}
		theDatabase.setMovieRating(input, output, rating);
	}
	
	/**
	 * Returns a 2-d array of the movies rated for a specific movie
	 */
	public String[][] getRatingsData(String movieTitle){
		if(!isInDatabase(movieTitle)){
			String[][] movieArray = {{"Not in database","Click Add Movie"}};
			return movieArray;
		}
		return theDatabase.getMovieRecs(movieTitle);
	}
	//**************User Window Methods*********************
	/**
	 * Returns an array of a specific user's history
	 */
	public String[] getUserHistory(int userID){
		if(userID<=0){
			String[] userArray ={"Invalid User"};
			return userArray;
		}
		return theDatabase.getUserHistory(userID);
	}
	/**
	 * Add a given movie to a specific user's history
	 */
	public void addMovieToUser(int userID, String movieTitle){
		if(userID<=0 || !isInDatabase(movieTitle)){
			return;
		}
		theDatabase.addMovieToUserHistory(userID, movieTitle);
	}
	/**
	 * Delete a given movie from a specific user's history
	 */
	public void deleteMovieFromUser(int userID, String movieTitle){
		if(userID<=0 || !isInDatabase(movieTitle)){
			return;
		}
		theDatabase.deleteMovieFromUserHistory(userID, movieTitle);
	}
	//*****************Private Helper Methods******************
	/**
	 * Determines if the database contains the movie
	 */
	private boolean isInDatabase(String movieTitle){
		return (theDatabase.getMovieIndex(movieTitle) > 0);
	}
}
